<?php

/**
 * Created by PhpStorm.
 * User: Evan
 * Date: 15/12/28
 * Time: 下午4:37
 */
class Public_cate extends MY_Agent_Controller
{
    public function index()
    {
        $limit = 200;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $this->load->model('Model_public_cate', 'pc');

        $totalNum = $this->pc->count();
        $cateInfo = $this->pc->lists($offset, $limit);


        $this->load->library('pagination');
        $config = $this->page_config('Public_cate/index', $totalNum, $limit);
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $data = array(
            'title'     => '公众号分类列表',
            'cate_info' => sortData($cateInfo),
            'pageHtml'  => $pageHtml,
            'totalNum'  => $totalNum,
        );


        $this->load->view('agent/public/cate_index', $data);
    }


    public function create()
    {
        $cid = (int)$this->uri->segment(3);

        $this->load->model('Model_public_cate', 'pc');
        $cateData = $this->pc->lists(0, 10000);

        $item = array();
        if ($cid)
            $item = $this->pc->getById($cid);


        $data = array(
            'title'     => '编辑分类',
            'data'      => $item,
            'cate_data' => sortData($cateData),
        );

        $this->load->view('agent/public/cate_create', $data);
    }


    public function save()
    {
        $cid = (int)$this->input->get_post('cid');


        $data = array(
            'name' => trim($this->input->get_post('name')),
            'parent_id' => (int)$this->input->get_post('parent_id'),
            'sort' => (int)$this->input->get_post('sort'),
            'title' => trim($this->input->get_post('title')),
            'keyword' => trim($this->input->get_post('keyword')),
            'descr' => trim($this->input->get_post('descr')),
            'depth' => trim($this->input->get_post('depth')),
        );


        if (empty ($data['name']))
            show_error('分类名称为空!');


        $this->load->model('Model_public_cate', 'pc');
        $rs = $this->pc->save($data, $cid ? $cid : null);

        if (!$rs)
            show_error('保存分类失败!');


        $this->load->helper('url');
        redirect(url('admin').'Public_cate/index');
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $cid = (int)$this->input->get_post('cid');

            if (!$cid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_public_cate', 'pc');
            $item = $this->pc->getById($cid);

            if (empty ($item))
            {
                $response = error(300000);
                break;
            }


            $this->load->model('Model_public', 'public');
            $item = $this->public->getOne(array('cid' => $cid));
            if (!empty ($item))
            {
                $response = error(300001);
                break;
            }


            $res = $this->pc->delete(array('cid' => $cid));
            if (!$res)
            {
                $response = error(300002);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}